<?php

namespace IDock\Insurance\ZSIC\Request;

use IDock\Insurance\ZSIC\Response\VehicleActualValueResponse;
use IDock\Insurance\ZSIC\ZSICEnums;
use IDock\Visitor\VehicleActualValueVisitor;

/**
 * 车辆实际价值计算
 */
class VehicleActualValueRequest extends AllZSICRequest
{

    public $requestCode = 'vehicleActualValueQuery';

    protected $response = VehicleActualValueResponse::class;

    protected $urlTip = '车辆实际价值计算';

    /**
     * @var VehicleActualValueVisitor $visitor
     */
    protected $visitor;

    public function generate(): array
    {
        ///**@var $premiumVehicleVisitor PremiumVehicleVisitor **/

        //$premiumVehicleVisitor = $this->visitor->argc['premiumVehicleVisitor'] ?? '';

        //if(empty($premiumVehicleVisitor)){
        //    throw new \Exception("保行通提示：配置异常");
        //}

        $this->channelCode = $this->visitor->argc['channelCode'] ?? '';
        $this->cityCode = $this->visitor->argc['cityCode'] ?? '';

        $data = [
            'base' => [
                'quoteType' => '2',//2-关联出单
                'vciEffectiveDate' => $this->visitor->startDate,
                'tciEffectiveDate' => $this->visitor->startDate,
            ],
            'vehicle' => [
                'idType' => ($this->visitor->argc['ownerIdType'] ?? '') ? ZSICEnums::$certificateTypeCode[$this->visitor->argc['ownerIdType']] : '',//车主证件类型
                'idNumber' => $this->visitor->argc['ownerIdCard'],
                'engineNo' => $this->visitor->argc['engineNo'],
                'frameNo' => $this->visitor->argc['frameNo'],
                'brandName' => $this->visitor->argc['modelName'],
                'printBrandName' => '',
                'firstRegistrationDate' => $this->visitor->argc['firstRegisterDate'],
                'modelCode' => $this->visitor->argc['modelCode'],
                'purchasePrice' => $this->visitor->vehiclePrice,
                'seat' => $this->visitor->vehicleSeat,
                'capacity' => (float)($this->visitor->argc['exhaustCapability']) * 1000,
                'importFlag' => $this->visitor->argc['importFlag'],
                'weight' => $this->visitor->argc['wholeWeight'],
                'tonnage' => $this->visitor->vehicleTonnage,
                'transferFlag' => $this->visitor->argc['isTrans'],
                'transferDate' => $this->visitor->argc['transDate'],
                'issueDate' => $this->visitor->argc['issueDate'],
                'vehicleType' => $this->getVehicleType($this->visitor->carKindCode,$this->visitor->vehicleSeat,$this->visitor->vehicleTonnage),

                'businessType' => ZSICEnums::$useNatureCode[$this->visitor->useNatureCode] ?? '',
                'pmOriCode' => $this->visitor->vehicleStyleCode,
                'licenseColor' => ZSICEnums::$licenseColorCode[$this->visitor->argc['licenseColorCode']] ?? '',
                'ownershipType' => ZSICEnums::$attachNature[$this->visitor->argc['attachNature']] ?? '',
                'purchaseDate' => '',//purchaseDate
                'useType' => ZSICEnums::$causeType[$this->visitor->causeType] ?? '',
                'drivingArea' => '',
                'vehicleCategory' => ZSICEnums::$carKindCode[$this->visitor->carKindCode] ?? '',
                'loanFlag' => '0',
                'customerType' => $this->visitor->argc['ownerTypeCode'] == 'R1' ? '1' : '2',
                'licenseType' => ZSICEnums::$licenseTypeCode[$this->visitor->argc['licenseTypeCode']] ?? '',
                'fuelType' => ZSICEnums::$energyType[$this->visitor->argc['energyType']] ?? '',
            ],
        ];
        if ($this->visitor->isNewEnergy) {
            $data['base']['vciProductVersion'] = 'NE';//新能源
        }

        return $data;
    }

    public function getVehicleType($carKindCode,$seat,$vehicleTonnage): string
    {
        $code = '5';

        if ($carKindCode == '200') {
            //货车
            $weight = round(round($vehicleTonnage, 3) / 1000, 2);

            if ($weight <= 2) {
                $code = '6';
            } elseif ($weight < 5) {
                $code = '7';
            } elseif ($weight < 10) {
                $code = '8';
            } elseif ($weight > 10) {
                $code = '9';
            }
        } else {
            //客车
            if ($seat <= 5) {
                $code = '1';
            } elseif ($seat < 10) {
                $code = '2';
            } elseif ($seat < 20) {
                $code = '3';
            } elseif ($seat < 36) {
                $code = '4';
            } else {
                $code = '5';
            }
        }

        return $code;
    }

    public function getBusinessNo()
    {
        return $this->visitor->argc['businessNo'] ?? '';
    }
}
